<template>
  <a-card class="task-card" :bordered="false" :bodyStyle="{padding:'0px 10px 10px 10px'}">
    <!-- 查询区域 -->
    <div class="table-operator">
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="fieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
    </div>
    <!-- table区域-begin -->
    <div>
      <!-- <a-tabs default-active-key="myTask" size="small" @change="tabChange">
        <a-tab-pane key="myTask">
          <span slot="tab">
            <a-icon type="user" />
            待办任务
          </span>
        </a-tab-pane>
        <a-tab-pane key="historyTask">
          <span slot="tab">
            <a-icon type="clock-circle" />
            已办任务
          </span>
        </a-tab-pane>
      </a-tabs> -->
      <div class="ant-alert ant-alert-info clearfix" style="margin-bottom: 16px;">
        <span style="float:right;">
          <a @click="refresh"><a-icon type="sync" />刷新</a>
          <a-divider type="vertical" />
          <col-setting v-if="defColumns.length>0" :defColumns="defColumns" v-model="settingColumns" />
        </span>
      </div>
      <a-table
        size="middle"
        bordered
        rowKey="formDataId"
        :columns="getColumns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="switch" slot-scope="text">
          <a-switch :checked="text" :disabled="true"/>
        </template>
        <template slot="uploadImg" slot-scope="text">
          <div v-if="Array.isArray(text) && text.length > 0" class="j-table-slot-view">
            <img v-for="(item,imgIndex) of text" class="upload-img" :src="item.url" alt=" " :key="imgIndex" :preview="index" v-show="imgIndex===0"/>
          </div>
        </template>
        <template slot="uploadFile" slot-scope="text">
          <div v-if="Array.isArray(text) && text.length > 0" class="j-table-slot-view">
            <a-button type="primary" icon="download" size="small" ghost @click="downloadRowFile(text)">下载</a-button>
          </div>
        </template>
        <template slot="user" slot-scope="text">
          <span>{{getRealname(text)}}</span>
        </template>
        <template slot="dept" slot-scope="text">
          <span>{{initDepartComponent(text)}}</span>
        </template>
        <template slot="action" slot-scope="text, record">
          <a v-if="record.taskType === 1" @click="handleClick(record)">办理</a>
          <a-divider v-if="record.taskType === 1" type="vertical"/>
          <a v-if="record.taskType === 1" @click="handleEntrustClick(record)">委托</a>
          <a-popconfirm v-if="record.taskType === 0" title="确定拾取任务吗?" @confirm="() => handleClaimTask(record)">
            <a >拾取</a>
          </a-popconfirm>
          <a-divider type="vertical"/>
          <a @click="handleHistory(record)">详情</a>
        </template>
      </a-table>
    </div>
    <!-- 任务办理弹窗 -->
    <task-modal ref="taskModal" :title="taskTitle" :taskData="taskData" @submit="completeTaskSubmit"></task-modal>
    <!-- 任务委托弹窗 -->
    <task-entrust-modal ref="taskEntrustModal" @ok="taskEntrust"></task-entrust-modal>
  </a-card>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { ProcessMixins } from './mixins/ProcessMixins'
import { getAction } from '@/api/manage'
import { entrustTask, claimTask, completeTask } from '@/api/task'
import taskEntrustModal from '@/views/modules/task/modal/taskEntrustModal'
import { filterDictTextByCache } from '@/components/dict/JDictSelectUtil'
export default {
  name: 'TaskList',
  components: {
    taskEntrustModal
  },
  mixins: [JeecgListMixin, ProcessMixins],
  data() {
    return {
      url: {
        list: '/activiti/task/getMyAllTaskList',
        deleteBatch: '/activiti/formData/deleteBatch',
        delete: '/activiti/formData/delete',
        getNewKfdJson: '/activiti/form/getFormDeploymentByModelId' // 自定义表单查询
      },
      taskTitle: '',
      rootUrLReplace: '/TaskList/', // 根据这个截取url参数
      statusColumns: {
        title: '进展',
        align: 'center',
        dataIndex: 'status',
        customRender: (text) => {
          return filterDictTextByCache('bpmn_status', text)
        }
      },
      hasNodeName: true
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    loadData(arg) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      // 加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams()// 查询条件
      params.createBy = this.userInfo().username// 添加用户查询的条件
      params.type = 1
      params.modelId = this.id
      this.loading = true
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records
          this.dataSource.forEach((item) => {
            item.formData = item.formData ? JSON.parse(item.formData) : ''
            this.setUserName(item.formData)
          })
          this.getUserList()
          this.ipagination.total = res.result.total
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
        this.loading = false
      })
    },
    handleClick(data) { // 办理
      this.taskData = data
      this.taskTitle = '【' + data.modelName + '】流程办理'
      this.$refs.taskModal.editModalVisible = true
    },
    handleEntrustClick(data) { // 委托
      this.taskData = data
      this.$refs.taskEntrustModal.modalVisible = true
    },
    taskEntrust(data) { // 委托保存
      if (!data) {
        this.$message.error('请先选择委托人')
        return false
      }
      entrustTask({ taskId: this.taskData.id, userId: data }).then((res) => {
        if (res.success) {
          this.$message.success('流程委托成功')
          this.loadData()
          this.$refs.taskEntrustModal.modalVisible = false
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleClaimTask(record) { // 拾取流程
      claimTask({
        taskId: record.id
      }).then(res => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    },
    handleHistory(data) { // 查看详情
      this.taskData = data
      this.$refs.taskModal.editModalVisible = true
    },
    completeTaskSubmit(data, appendFormData) { // 流程审批
      completeTask({
        comment: data.reason,
        taskId: this.taskData.id,
        procInstId: this.taskData.processInstanceId,
        formDataId: this.taskData.formDataId,
        usernames: data.ccUserRealNames,
        flag: data.flag,
        appendFormId: this.taskData.appendForm && this.taskData.appendForm.id,
        formData: this.taskData.appendForm && JSON.stringify(appendFormData),
        appendFormDataId: data.appendFormDataId,
        nodeId: data.nodeId,
        files: data.files
      }).then(res => {
        if (res.success) {
          this.$message.success('流程办理成功')
          this.loadData()
          this.$refs.taskModal.editModalVisible = false
        } else {
          this.$message.error(res.message)
        }
      }).catch((error) => {
        console.log(error)
      })
    },
    tabChange() {

    }
  }
}
</script>
<style>
  .task-card{
    padding-top:10px;
  }
</style>
<style lang="less" scoped>
@import '~@assets/less/common.less';
  .j-table-slot-view{
    font-size: 12px;
    font-style: italic;
    .upload-img {
      max-width: 80px;
      max-height: 18px;
    }
  }
  .j-table-slot-view img {
    max-width: 80px;
    max-height: 25px;
  }
  .clearfix:after{
    content:"";
    display:block;
    visibility:hidden;
    clear:both;
  }
  .ant-card-body .table-operator{
    margin-bottom: 0px;
  }

</style>
